package com.hwadee.property.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.hwadee.property.pojo.House;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Repository
@Mapper
public interface HouseMapper extends BaseMapper<House> {
    // 统计符合条件的房屋总数
    @Select("<script>" +
            "SELECT COUNT(*) FROM house h" +
            " LEFT JOIN building b ON h.building_id = b.id" +
            "<where>" +
            "   <if test='communityId != null'> AND b.community_id = #{communityId} </if>" +
            "   <if test='buildingId != null'> AND h.building_id = #{buildingId} </if>" +
            "   <if test='ownerId != null'> AND h.owner_id = #{ownerId} </if>" +
            "   <if test='floor != null'> AND h.floor = #{floor} </if>" +
            "   <if test='roomNum != null'> AND h.room_num = #{roomNum} </if>" +
            "   <if test='roomNumber != null and roomNumber.trim() != \"\"'> " +
            "       AND h.room_number = #{roomNumber} " +
            "   </if>" +
            "   <if test='remark != null and remark.trim() != \"\"'> " +
            "       AND h.remark LIKE CONCAT('%', #{remark}, '%') " +
            "   </if>" +
            "   <if test='startDate != null'> AND h.check_in_date &gt;= #{startDate} </if>" +
            "   <if test='endDate != null'> AND h.check_in_date &lt;= #{endDate} </if>" +
            "</where>" +
            "</script>")
    int countHouses(@Param("communityId") Long communityId,
                    @Param("buildingId") Long buildingId,
                    @Param("ownerId") Long ownerId,
                    @Param("floor") Integer floor,
                    @Param("roomNum") Integer roomNum,
                    @Param("roomNumber") String roomNumber,
                    @Param("remark") String remark,
                    @Param("startDate") Date startDate,
                    @Param("endDate") Date endDate);

    // 分页查询符合条件的房屋列表
    @Select("<script>" +
            "SELECT h.* FROM house h" +
            " LEFT JOIN building b ON h.building_id = b.id" +
            "<where>" +
            "   <if test='communityId != null'> AND b.community_id = #{communityId} </if>" +
            "   <if test='buildingId != null'> AND h.building_id = #{buildingId} </if>" +
            "   <if test='ownerId != null'> AND h.owner_id = #{ownerId} </if>" +
            "   <if test='floor != null'> AND h.floor = #{floor} </if>" +
            "   <if test='roomNum != null'> AND h.room_num = #{roomNum} </if>" +
            "   <if test='roomNumber != null and roomNumber.trim() != \"\"'> " +
            "       AND h.room_number = #{roomNumber} " +
            "   </if>" +
            "   <if test='remark != null and remark.trim() != \"\"'> " +
            "       AND h.remark LIKE CONCAT('%', #{remark}, '%') " +
            "   </if>" +
            "   <if test='startDate != null'> AND h.check_in_date &gt;= #{startDate} </if>" +
            "   <if test='endDate != null'> AND h.check_in_date &lt;= #{endDate} </if>" +
            "</where> " +
            "ORDER BY h.check_in_date ASC " +
            "LIMIT #{size} OFFSET #{offset}" +
            "</script>")
    List<House> getHouses(@Param("communityId") Long communityId,
                          @Param("buildingId") Long buildingId,
                          @Param("ownerId") Long ownerId,
                          @Param("floor") Integer floor,
                          @Param("roomNum") Integer roomNum,
                          @Param("roomNumber") String roomNumber,
                          @Param("remark") String remark,
                          @Param("startDate") Date startDate,
                          @Param("endDate") Date endDate,
                          @Param("size") int size,
                          @Param("offset") int offset);
}
